import { defineStore } from 'pinia'
import type { UserState } from '@/stores/modules/types/userTypes.ts'
import { dynamicRouter } from '@/router/dynamicRouter.ts'
import {getLoginData, handleLogout} from '@/api/login'

const getUserInfo = defineStore('userInfoStore', {
  state: (): UserState => {
    return {
      token: null,
      routes: dynamicRouter,
      userInfo:{}
    }
  },
  actions:{
    // 登录 保存token 以及登录用户信息
    async login(UserInfo: { username: string; password: string }) {
       const res= await getLoginData(UserInfo)
        if (res.code === 200) {
          this.token = res.data.token
          this.userInfo={...res.data.userInfo}
          return res.data
        } else {
          return Promise.reject(res.message)
        }
    },
    async logout() {
     const res= await handleLogout()
      if (res.code === 200) {
        // 退出登录逻辑处理
        this.token = null
        this.userInfo = {}
        // 这里可以添加其他退出登录的逻辑，比如清除缓存等
        return Promise.resolve('退出成功')
      } else {
        return Promise.reject(res.message)
      }
    }
  },
  //是否进行持久化存储
  persist:true
})

export default getUserInfo
